package com.ly.lypopupwindowdemo.activity;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.MyViewPagerAdapter;

import java.lang.reflect.Field;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;


public class TabLayoutActivity1 extends AppCompatActivity {

    //绑定控件
    @BindView(R.id.tablayout)
    public TabLayout tabLayout;
    //绑定string里面的array数组
    @BindArray(R.array.titlename)
    String[] titles;

    @BindView(R.id.viewpager)
    public ViewPager viewpager;

    LinearLayout mLinearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_layout1);

        ButterKnife.bind(this);

//        for (int i=0;i<titles.length;i++){
//            TabLayout.Tab tab = tabLayout.newTab();
//            tab.setText(titles[i+1]);
//            tabLayout.addTab(tab);
//        }

        mLinearLayout = (LinearLayout) tabLayout.getChildAt(0);
        //在所有子控件中间显示分割线
        mLinearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        /**
         * // 设置分割线的距离本身（LinearLayout）的内间距
         mLinearLayout.setDividerPadding(20);
         // 设置分割线的样式
         mLinearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider_vertical));
         */

        mLinearLayout.setDividerPadding(40);
        mLinearLayout.setDividerDrawable(ContextCompat.getDrawable(this, R.drawable.divider_vertical));

        /**
         * app:tabBackground="@color/colorPrimaryDark"只是给Tab设置了背景色，
         * 而不是给Tab的父级控件LinearLayout设置，这个LinearLayout默认的背景色是白色，
         * 所以才会是这个样子，解决方法自然就是给LinearLayout设置跟Tab一样的背景色就好了。
         */
        mLinearLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));


//        // 设置LinearLayout中子View(Tab)的外间距
//        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics());
//        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, Resources.getSystem().getDisplayMetrics());
//        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
//            View tabView = mLinearLayout.getChildAt(0);
//            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
//            params.leftMargin = left;
//            params.rightMargin = right;
//            tabView.setLayoutParams(params);}

        setIndicator(tabLayout,20,20);

            /**
             * 给tab设置点击事件
             * tablayout不一定要和viewpager一起使用，可以单纯设置其点击事件
             */

            tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    Toast.makeText(TabLayoutActivity1.this, titles[tab.getPosition()], Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });


            MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager(), titles);
            viewpager.setAdapter(adapter);


            /**
             *  MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager());
             mViewPager.setAdapter(adapter);

             // 适配器必须重写getPageTitle()方法
             mTabLayout.setTabsFromPagerAdapter(adapter);
             // 监听TabLayout的标签选择，当标签选中时ViewPager切换
             mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
             // 监听ViewPager的页面切换，当页面切换时TabLayout的标签跟着切换
             mViewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

             以上一堆代码可以简化成一句代码
             mTabLayout.setupWithViewPager(mViewPager);
             */

            tabLayout.setupWithViewPager(viewpager);

        }




    // 具体方法（通过反射的方式）
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
    }

